
package com.x52im.rbchat.logic.chat_root.impl;

import android.app.Activity;
import android.view.View;
import android.widget.TextView;

import com.eva.epc.common.util.CommonUtils;

/**
 * 当消息列表最后一行处于可见或不可见时的新消息（未读消息）提示ui包装实现类.
 *
 * @author Jack Jiang
 * @since 2.5.1
 */
public class UnreadMessageBallonWrapper
{
    private Activity parentActivity = null;
    /**
     * 消息提示气泡样文本组件
     */
    private TextView viewUnreadBallon = null;

    /**
     * 未读消息提示气泡的文本组件资源id
     */
    private int resid_unreadBallonBtn = -1;

    public UnreadMessageBallonWrapper(Activity parentActivity, int resid_unreadBallonBtn)
    {
        this.parentActivity = parentActivity;
        this.resid_unreadBallonBtn = resid_unreadBallonBtn;

        initViews();
        initListeners();
    }

    private void initViews()
    {
        viewUnreadBallon = (TextView) parentActivity.findViewById(this.resid_unreadBallonBtn);
    }

    private void initListeners()
    {
        viewUnreadBallon.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                fireOnClickBallon();
            }
        });
    }

    /**
     * 当点击未读气泡时要调用的方法.
     */
    protected void fireOnClickBallon()
    {
        // default do nothing
    }

    /**
     * 设置当前总的未读数.
     *
     * @param unreadCount 总未读数
     */
    private void setUnreadCount(int unreadCount)
    {
        if (unreadCount < 0)
            unreadCount = 0;

        // 未读数是否超过了99？
        boolean over99 = unreadCount > 99;
        // 显示未读数（如果超过了99条，则以“99+”的样式显示出来）
        viewUnreadBallon.setText(String.valueOf(over99 ? 99 : unreadCount) + (over99 ? "+" : ""));

        // 根据当前未读总数来决定这个未读数ui组件的可见性
        if (unreadCount > 0)
            viewUnreadBallon.setVisibility(View.VISIBLE);
        else
            viewUnreadBallon.setVisibility(View.GONE);
    }

    /**
     * 重置总的未读数.
     */
    public void resetUnreadCount()
    {
        setUnreadCount(0);
    }

    /**
     * 返回当前的未读数.
     *
     * @return
     */
    private int getUnreadCount()
    {
        return CommonUtils.getIntValue(String.valueOf(viewUnreadBallon.getText()));
    }

    /**
     * 总未读数累加.
     *
     * @param countForAccumulate 要累加的值
     */
    public void addUnreadCount(int countForAccumulate)
    {
        setUnreadCount(countForAccumulate + getUnreadCount());
    }
}